package com.wiegand.content.background {
	import com.caurina.transitions.Tweener;

	import flash.display.CapsStyle;
	import flash.display.LineScaleMode;
	import flash.display.Sprite;
	import flash.geom.Point;

	/**
	 * @author gandy
	 */
	public class Line extends Sprite {
		
		private var _points:Array = new Array();
		private var _point:Point = new Point();
		private var _segments:int;
		private var _current:int=0;
		private var _xml : XMLList;
		
		public static const EVENT_OUTOFRANGE : String = "out of range";

		public function Line(xml:XMLList) {
			_xml=xml;
			_segments = _xml.children().length();
			_point = new Point(_xml.@startX, _xml.@startY);				
			_points.push( _point );
			graphics.lineStyle(int(_xml.@thickness), uint(_xml.@color), Number(_xml.@alpha), false, LineScaleMode.NORMAL, CapsStyle.ROUND);
			startTween();
		}
		
		public function stop():void{
			Tweener.removeTweens(_point);
		}
		
		private function startTween() : void {
			
			if(_current<_segments){
				var newX : int = int(_xml.child(_current).@x);
				var newY : int = int(_xml.child(_current).@y);
				var time : Number = Number(_xml.child(_current).@time);
				var delay : Number = Number(_xml.child(_current).@delay);
				var easing : String = _xml.child(_current).@transition;
					
				Tweener.removeTweens(_point);
				Tweener.addTween(_point,{x:newX, y:newY, delay:delay, time:time, transition:easing, onUpdate:onUpdatePointTween, onComplete:startTween });
			
				_current++;	
			}
		}
		
		private function onUpdatePointTween():void{			
			graphics.moveTo ( Point(_points[_points.length-1]).x, Point(_points[_points.length-1]).y );
			graphics.lineTo (_point.x, _point.y);
	 		_points.push( new Point( _point.x, _point.y ) );
		}
		
		public function get point():Point{
			return _point;
		}
		
		public function get points():Array{
			return _points;
		}
		
		public function clear():void{
			Tweener.removeTweens(_point);
			graphics.clear();	
		}
		
	}
}
